package com.chun.controller;

import com.chun.pojo.Clazz;
import com.chun.pojo.PageBean;
import com.chun.pojo.Result;
import com.chun.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Clazz请求类
 */
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result getClazzs(String name, @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = clazzService.getClazzs(name, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{id}")
    public Result deleteClazzById(@PathVariable Integer id) {
        clazzService.deleteClazzById(id);
        return Result.success();
    }

    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        clazzService.addClazz(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id) {
        Clazz clazz = clazzService.getClazzById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getClazzList() {
       List<Clazz> clazzList = clazzService.getClazzList();
       return Result.success(clazzList);
    }

}
